﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CRM.Models;

namespace CRM.Controllers
{
    [Authorize]
    public class CompanyController : Controller
    {
        private CRMdbEntities respository;

        /// <summary>
        ///  构造函数
        /// </summary>
        public CompanyController()
        {
            respository = new CRMdbEntities();
        }
        //
        // GET: /Company/

        public ActionResult Index()
        {
            return View(respository.Company);
        }

        //
        // GET: /Company/Details/5

        public ActionResult Details(Guid id)
        {
            return View(respository.Company.Where(c=>c.Id == id).First());
        }

        //
        // GET: /Company/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Company/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Company/Edit/5

        //[Authorize(Users = "Stephen,wdfs")]                       //指定用户有操作权限，否则跳转到登陆页,多个用户用逗号隔开
        [Authorize(Roles = "Manager,Employee")]                     //具有指定角色的用户具有操作权利
        public ActionResult Edit(Guid id)
        {
            return View(respository.Company.Where(c => c.Id == id).First());
        }

        //
        // POST: /Company/Edit/5

        [HttpPost]
        public ActionResult Edit(Guid id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
